package com.example.backstage.controller;

import cn.hutool.core.io.resource.ClassPathResource;
import com.example.backstage.service.IFileService;
import com.example.backstage.service.ProductService;
import com.example.backstage.service.UserService;
import com.example.backstage.vo.ProductListVo;
import com.example.gok_mall.constUtil.PropertiesUtil;
import com.example.gok_mall.entity.Product;
import com.example.gok_mall.entity.User;
import com.example.gok_mall.reponseData.ResultData;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Maps;
import io.swagger.annotations.Api;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.Map;
import java.util.UUID;

@Controller
@RestController
@RequestMapping("/product")
@ResponseBody
@Api(value = "产品接口类")
public class ProductController {
    @Autowired
    public ProductService productService;

    @Autowired
    private UserService userService;

    @Autowired
    private IFileService iFileService;

    //实现产品更新 //实现产品添加
    @PostMapping("/update")
    public ResultData productUpdate(HttpSession session, Product product) {
        User user = (User) session.getAttribute("userInfo");
        if (user == null) {
            return ResultData.fail("请登录管理员账户，登录后再试");
        }
        if (userService.checkAdminRole(user) != null) {
            return productService.UpdateAndInsertProduct(product);
        } else {
            return ResultData.fail("当前登录者不是管理员，无权限操作");
        }
    }

    //实现产品删除
    @DeleteMapping("/delete")
    public ResultData productDelete(HttpSession session, Integer id) {
        User user = (User) session.getAttribute("userInfo");
        if (user == null) {
            return ResultData.fail("请登录管理员账户，登录后再试");
        }
        if (userService.checkAdminRole(user) != null) {
            return productService.deleteProduct(id);
        } else {
            return ResultData.fail("当前登录者不是管理员，无权限操作");
        }
    }

    //更新商品的在售状态
    @PostMapping("/set_sale_status.do")
    public ResultData setSaleStatus(HttpSession session, Integer id, Integer status) {
        User user = (User) session.getAttribute("userInfo");
        if (user == null) {
            return ResultData.fail("请登录管理员账户，登录后再试");
        }
        if (userService.checkAdminRole(user) != null) {
            return productService.setSaleStatus(id, status);
        } else {
            return ResultData.fail("当前登录者不是管理员，无权限操作");
        }
    }

    //获取商品列表接口
//    @GetMapping("/get_list.do")
//    public ResultData getList(@RequestParam(defaultValue = "1") Integer pageNum,
//                              @RequestParam(defaultValue = "10") Integer pageSize) {
//        PageInfo pageInfo = (PageInfo) productService.getProductList(pageNum,pageSize);
//        ProductListVo productListVo = new ProductListVo();
//        BeanUtils.copyProperties(pageInfo,productListVo);
//        System.out.printf(pageInfo.toString());
//        System.out.printf("====================================");
//        return ResultData.createData(productListVo);
//    }
    //获取商品列表接口
    @GetMapping("/get_list")
    public ResultData getList(HttpSession session, @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum, @RequestParam(value = "pageSize",defaultValue = "10") Integer pageSize){
        User user = (User) session.getAttribute("userInfo");
        if (user == null) {
            return ResultData.fail("请登录管理员账户，登录后再试");
        }
        if (userService.checkAdminRole(user) != null) {
            //增加商品的逻辑方法
            return (ResultData) productService.getProductList(pageNum,pageSize);
        }else {
            return ResultData.fail("当前登录者不是管理员，无权限操作");
        }
    }

    //获取商品详情接口
    @GetMapping("/get_detail.do")
    public ResultData getDetail(HttpSession session, Integer productId) {
        User user = (User) session.getAttribute("userInfo");
        if (user == null) {
            return ResultData.fail("请登录管理员账户，登录后再试");
        }
        if (userService.checkAdminRole(user).isSuccess()) {
            return productService.manageProductDetail(productId);
        } else {
            return ResultData.fail("当前登录者不是管理员，无权限操作");
        }
    }
    //后台产品搜索接口
    //获取商品列表接口
    @GetMapping("/search.do")
    public ResultData search(HttpSession session,String productName, Integer productId, @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum, @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        User user = (User) session.getAttribute("userInfo");
        if (user == null) {
            return ResultData.fail("请登录管理员账户，登录后再试");
        }
        if (userService.checkAdminRole(user).isSuccess()) {
            //搜索商品的逻辑方法
            return  productService.searchProduct(productName,productId,pageNum,pageSize);
        }else {
            return ResultData.fail("当前登录者不是管理员，无权限操作");
        }
    }

    //springmvc文件上传接口
    @PostMapping("/upload.do")
    public ResultData upload(HttpSession session, @RequestParam(value = "upload_file",required = false) MultipartFile file, HttpServletRequest request) {
        User user = (User) session.getAttribute("userInfo");
        if (user == null) {
            return ResultData.fail("请登录管理员账户，登录后再试");
        }
        if (userService.checkAdminRole(user).isSuccess()) {
            String path = request.getSession().getServletContext().getRealPath("upload");
            String targetFileName = iFileService.upload(file, path);
            String url = PropertiesUtil.getProperty("ftp.server.http.prefix") + targetFileName;

            Map fileMap = Maps.newHashMap();

            fileMap.put("uri", targetFileName);
            fileMap.put("url", url);
            return ResultData.createData(fileMap);

        } else {

        }
            return ResultData.fail("当前登录者不是管理员，无权限操作");
        }
    }
